using Microsoft.EntityFrameworkCore;
using Microsoft.OpenApi.Models;
using PandaWebAPI.Middleware;
using PandaWebAPI.Models.ef;
using PandaWebAPI.Services;
using PandaWebAPI.Interfaces;

namespace PandaWebAPI
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);

            // 添加CORS服务
            builder.Services.AddCors(options =>
            {
                options.AddPolicy("AllowVueApp",
                    builder => builder
                        .WithOrigins(
                            "http://localhost:5173",  // Vite 默认端口
                            "http://localhost:8080"   // Vue CLI 默认端口
                        )
                        .AllowAnyMethod()
                        .AllowAnyHeader()
                        .AllowCredentials());
            });

            builder.Services.AddDbContext<PandaSystemContext>(options =>
                options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));

            // Add services to the container.
            builder.Services.AddScoped<IUserService, UserService>();
            builder.Services.AddScoped<ICartItemService, CartItemService>();
            builder.Services.AddScoped<ICategoryService, CategoryService>();
            builder.Services.AddScoped<ICommentService, CommentService>();
            builder.Services.AddScoped<IProductService, ProductService>();
            builder.Services.AddScoped<ILikeService, LikeService>();
            builder.Services.AddScoped<ILoginSessionService, LoginSessionService>();
            builder.Services.AddScoped<IOrderService, OrderService>();
            builder.Services.AddScoped<IOrderItemService, OrderItemService>();
            builder.Services.AddScoped<IPandaService, PandaService>();
            builder.Services.AddScoped<IPandaBasisService, PandaBasisService>();
            builder.Services.AddScoped<IPostService, PostService>();
            builder.Services.AddScoped<IPostTagService, PostTagService>();
            builder.Services.AddScoped<IShoppingCartService, ShoppingCartService>();
            builder.Services.AddScoped<ITagService, TagService>();
            builder.Services.AddScoped<ISiteStatisticService, SiteStatisticService>();
            builder.Services.AddScoped<IVerificationCodeService, VerificationCodeService>();
            builder.Services.AddControllers();
            // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
            builder.Services.AddEndpointsApiExplorer();
            
            // 配置Swagger
            builder.Services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo { 
                    Title = "Panda API", 
                    Version = "v1",
                    Description = "熊猫系统API文档"
                });
            });

            var app = builder.Build();

            // 启用CORS - 注意：必须在其他中间件之前
            app.UseCors("AllowVueApp");

            // 注册异常处理中间件
            app.UseMiddleware<ExceptionMiddleware>();

            // Configure the HTTP request pipeline.
            if (app.Environment.IsDevelopment())
            {
                // 配置Swagger
                app.UseSwagger();
                app.UseSwaggerUI(c =>
                {
                    c.SwaggerEndpoint("/swagger/v1/swagger.json", "Panda API V1");
                    c.RoutePrefix = string.Empty; // 将Swagger设置为默认页面
                });
            }

            app.UseRouting();
            app.UseHttpsRedirection();
            app.UseAuthorization();
            // 简化路由配置
            app.MapControllers();

            // 添加一个测试端点
            app.MapGet("/", () => "Panda API is running!");

            app.Run();
        }
    }
}
